<?
class LinkedList {
	protected $parent;
	protected $childclass;
	protected $elements = Array();

	public function LinkedList($parent, $childclass){
		$this->parent = $parent;
		$this->childclass = $childclass;
	}

	public function restore(){
		$entry = new ListEntry();
		$this->elements = db::getList(ListEntry, array(array("parentid", "=", $this->parent->getId()), array("parentclass", "=", get_class($this->parent))), "index", false);
	}

	public function indexate(){
		for($i = 0;$i < count($this->elements); $i++){
			$this->elements[$i]->setIndex($i);
			db::save($this->elements[$i]);
		}
	}

	public function remove($persistable){
		for($i = 0;$i < count($this->elements); $i++){
			if($this->elements[$i]->getChild()->getId() == $persistable->getId()){
				db::delete($this->elements[$i]);
				unset($this->elements[$i]);
				$this->elements = array_values($this->elements);
				$this->indexate();
				return;
			}
		}
	}

	public function add($persistable){
		$entry = new ListEntry($this->parent);
		$entry->setChild($persistable);
		$entry->setIndex(count($this->elements));
		$this->elements[] = $entry;
	}

	public function get($index){
		return $this->elements[$index]->getChild();
	}

	public function size(){
		return count($this->elements);
	}

	public function save(){
		for($i = 0; $i < count($this->elements); $i++){
			db::save($this->elements[$i]);
		}
	}
}
/*
class ListEntry extends Persistable{
	public $mapping = array(
		"parentclass" => array("type" => "VARCHAR", "size" => "255"),
		"childclass" => array("type" => "VARCHAR", "size" => "255"),
		"parentid" => array("type" => "INT"),
		"childid" => array("type" => "INT"),
		"index" => array("type" => "INT"),
		"fixed" => array("type" => "BOOL"),
	);
	protected $parentclass;
	protected $childclass;
	protected $parentid;
	protected $childid;
	public $index;
	protected $fixed;

	public function ListEntry($parent = "", $child = ""){
		$this->fixed = false;
		$this->setParent($parent);
		$this->setChild($child);
	}

	public function setFixed($fixed){
		$this->fixed = $fixed;
	}

	public function getFixed(){
		return $this->fixed;
	}

	public function setIndex($index){
		$this->index = $index;
	}

	public function getIndex(){
		return $this->index;
	}

	public function getParent(){
		if(!isset($this->parentclass) || !isset($this->parentid)) return;
		return db::get($this->parentclass, $this->parentid);
	}

	public function setParent($persistable){
		if($persistable instanceof Persistable){
			$this->parentclass = get_class($persistable);
			$this->parentid = $persistable->getId();
		}
	}

	public function getChild(){
		if(!isset($this->childclass) || !isset($this->childid)) return;

		return db::get($this->childclass, $this->childid);
	}

	public function setChild($persistable){
		if($persistable instanceof Persistable){
			$this->childclass = get_class($persistable);
			$this->childid = $persistable->getId();
		}
	}
}*/
?>